package com.varbal.icegame.algorithms.elementdata;

/**
 * 自制数组线性表的研究
 */
public class ArrayList<T> {
    private T[] elementData;
    private int size;

    public ArrayList(int size){
        this.size = size;
        this.elementData = (T[]) new Object[size];
    }

    public T findKth(int index){
        return elementData[index];
    }

    public int firstFind(T t){
        for (int i = 0 ; i < elementData.length ; i++){
            if (null != t && t.equals(elementData[i])){
                return i;
            }
        }
        return -1;
    }

    public void insert(T t , int index){
        int tempSiza = this.size++;
        T[] tempDate = (T[]) new Object[tempSiza];
        for (int i = 0 ,temp = 0 ; i < tempSiza ; i++ , temp++){
            if (i == index) {
                tempDate[i] = t;
                temp++;
            }
            tempDate[temp] = elementData[i];
        }
        elementData = tempDate;
        size = tempSiza;
    }

    public void delete(int index){
        int tempSiza = this.size--;
        T[] tempDate = (T[]) new Object[tempSiza];
        for (int i = 0 ,temp = 0 ; i < size ; i++ , temp++){
            if (i == index) {
                i++;
            }
            tempDate[temp] = elementData[i];
        }
        elementData = tempDate;
        size = tempSiza;
    }

    public int length(){
        return size;
    }
}
